package com.net.xpay.web.controller.common;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.xpay.common.manager.LoginLogManager;
import com.net.xpay.web.secutiry.CurrentOperator;
import com.net.xpay.web.secutiry.OperatorStatus;
import com.net.xpay.web.vo.UserVo;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.UserProfile;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.UserProfileManager;
import com.net.xpay.common.model.PasswordModel;
import com.net.xpay.common.service.UserService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.AlipayUser;
import com.net.xpay.core.domain.UserAccount;
import com.net.xpay.core.enums.AlipayUserStatus;
import com.net.xpay.core.manager.AlipayUserManager;
import com.net.xpay.core.manager.UserAccountManager;
import com.net.xpay.core.model.UserTransLimitModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.web.secutiry.Operator;
import com.net.xpay.web.secutiry.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * on 10/02/2018.
 */
@RestController
@RequestMapping("/common/user")
@Api(tags = "公共/用户", description = "公共 用户 net")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private AlipayUserManager alipayUserManager;

    @Autowired
    private UserAccountManager userAccountManager;

    @Autowired
    private UserManager userManager;

    @Autowired
    private UserProfileManager userProfileManager;
    @Autowired
    private LoginLogManager loginLogManager;


    @ApiOperation(value = "获取当前用户")
    @GetMapping
    public ResultData<UserVo> getUser(@ApiIgnore @RequestUser User user) {
        if (user == null) {
            throw new BusinessException(PoseidonErrorCode.NEED_LOGIN, "登录已过期, 请重新登录");
        }

        UserVo userVo = new UserVo();
        BeanCopierUtil.copy(user, userVo);
        userVo.setHasPayPassword(!Strings.isNullOrEmpty(user.getPayPassword()));
        userVo.setAlipayUserStatus(AlipayUserStatus.NO);
        if (user.getIndustryCategory() != null) {
            userVo.setIndustry(user.getIndustryCategory().getDesc());
        }

        if (user.getAlipayUserId() != null) {
            AlipayUser alipayUser = alipayUserManager.getById(user.getAlipayUserId());
            userVo.setAlipayUserStatus(alipayUser.getStatus());
        }

        UserAccount userAccount = userAccountManager.getByIdWithLock(user.getId());
        if (userAccount == null) {
            userAccount = new UserAccount();
        }
        userVo.setMoneyLeft(userAccount.getMoneyLeft());
        userVo.setTransBatchLeft(userAccount.getTransBatchLeft());

        //配置信息
        UserProfile userProfile = userProfileManager.getById(user.getId());
        if (userProfile == null) {
            userProfile = userService.createUserProfile(user.getId());
        }
        userVo.setUserProfile(userProfile);

        if (!Strings.isNullOrEmpty(user.getAppkey())) {
            userVo.setShowDevSettings(true);
        } else {
            userVo.setShowDevSettings(false);
        }

        return new ResultData<>(userVo);
    }

    @ApiOperation(value = "切换支付宝账号")
    @PostMapping("/switch-alipay-user")
    public ResultData<Void> switchAlipayUser(@ApiIgnore @RequestUser User user, Long alipayUserId) {
        if (user == null) {
            throw new BusinessException(PoseidonErrorCode.NEED_LOGIN, "登录已过期, 请重新登录");
        }
        Preconditions.checkArgument(alipayUserId != null);
        AlipayUser alipayUser = alipayUserManager.findById(user.getId(), alipayUserId);
        if (alipayUser == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号不存在");
        }
        user.setAlipayUserId(alipayUser.getId());
        user.setAlipayNickName(alipayUser.getAlipayNickName());
        userManager.updateByPrimaryKeyWithVersion(user);

        return new ResultData<>();
    }

    @ApiOperation(value = "忘记密码")
    @PutMapping("/reset-password")
    public ResultData<Boolean> resetPassword(@ApiIgnore @RequestBody PasswordModel passwordModel) {
        if (Strings.isNullOrEmpty(passwordModel.getMobile())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号不能为空");
        }

        User user = userService.resetPassword(passwordModel);
        //找回密码
        return new ResultData<>(user != null);
    }

    @ApiOperation(value = "修改密码（登录后才能操作）")
    @PutMapping("/modify-password")
    public ResultData<Boolean> modifyPassword(@ApiIgnore @CurrentOperator Operator operator, @ApiIgnore @RequestUser User user, @RequestBody PasswordModel passwordModel) {
        if (operator == null) {
            throw new BusinessException(PoseidonErrorCode.NEED_LOGIN, "请先登录,登陆之后才能修改密码");
        }
        if (operator.getStatus() == OperatorStatus.OPEN_ID_NEED_BIND) {
            throw new BusinessException(PoseidonErrorCode.OPEN_ID_NEED_BIND, "请先绑定账号,绑定之后才能修改密码");
        }

        return new ResultData<>(userService.modifyPassword(user, passwordModel) != null);
    }

    @ApiOperation(value = "用户查询支付宝当天的转账额度和当月的转账额度")
    @GetMapping("/get-transfer-limit")
    public ResultData<UserTransLimitModel> getTransferLimit(@ApiIgnore @RequestUser User user) {
        UserTransLimitModel userTransLimitModel = userService.getTransferLimit(user, user.getAlipayUserId());
        return new ResultData<>(userTransLimitModel);
    }


}
